//
//  FSourceListView.m
//  SolarSeek
//
//  Created by Erick Fejta on 05/03/22.
//  Copyright 2005 Erick Fejta. All rights reserved.
//

#import "FSourceListView.h"


@implementation FSourceListElement
- (id)initWithView:(NSView *)view {
	if(self = [super init]) {
		_view = [view retain];
	}
	return self;
}

- (void)dealloc {
	[_view release];
	[super dealloc];
}

- (void)setView:(NSView *)view {
	[_view release];
	_view = [view retain];
}

- (NSView *)view {
	return _view;
}
@end

#pragma mark -

@implementation FSourceListArrayElement
- (id)initWithView:(NSView *)view {
	if(self = [super init]) {
		_view = [view retain];
	}
	return self;
}

- (void)dealloc {
	[_view release];
	[super dealloc];
}

- (void)setView:(NSView *)view {
	[_view release];
	_view = [view retain];
}

- (NSView *)view {
	return _view;
}
@end

#pragma mark -

@implementation FSourceListView 

- (id)init {
	if(self = [super init]) {
		_contentView = nil;
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentView:) name:NSOutlineViewSelectionDidChangeNotification object:nil];
	}
	return self;
}

- (id)initWithFrame:(NSRect)frameRect {
	return [self initWithFrame:frameRect contentView:nil];
}

- (id)initWithFrame:(NSRect)frameRect contentView:(NSView *)contentView {
	if(self = [super initWithFrame:frameRect]) {
		_contentView = [contentView retain];
		[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentView:) name:NSOutlineViewSelectionDidChangeNotification object:nil];
	}
	return self;
}

- (void)dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver:self name:NSOutlineViewSelectionDidChangeNotification object:self];
	[_contentView release];
	[super dealloc];
}


- (void)keyDown:(NSEvent *)event {
	unsigned short s = [event keyCode];
	id i = [self itemAtRow:[super selectedRow]];
	if(s == 51) {
		if([i respondsToSelector:@selector(removeFromSourceList)]) {
			[i removeFromSourceList];
		}
	}
	else {
		[[self nextResponder] keyDown:event];
	}
}

- (void)awakeFromNib {
    if ([[self superclass] instancesRespondToSelector:@selector(awakeFromNib)])
 		[super awakeFromNib];
	
	[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeContentView:) name:NSOutlineViewSelectionDidChangeNotification object:self];

	NSEnumerator *e = [[super tableColumns] objectEnumerator];
	NSTableColumn *tc;
	while(tc = [e nextObject]) {
		if([[tc identifier] isKindOfClass:[NSString class]]) {
			[_dataSource setTableColumn:tc performsSelector:NSSelectorFromString([tc identifier])];
		}
	}
}

- (void)changeContentView:(NSNotification *)note {
	int row = [super selectedRow];
	if(row >= 0) {
		FSourceListElement *fsle = [super itemAtRow:row];
		NSView *v = [fsle view];
		if([[_contentView subviews] count] > 0) {
			[[[_contentView subviews] lastObject] removeFromSuperview];
		}
		
		NSRect r = [_contentView frame];
		r.origin.x = r.origin.y = 0;
		[v setFrame:r];
		[_contentView addSubview:v];
		[_contentView display];
	}
}


- (NSView *)contentView { return _contentView; }
- (void)setContentView:(NSView *)view { [_contentView release]; _contentView = [view retain]; }


@end